package com.example.tian.securityinspection.base;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.annotation.StringRes;
import android.support.v7.widget.Toolbar;


import com.example.tian.securityinspection.R;
import com.example.tian.securityinspection.ui.Display;
import com.example.tian.securityinspection.util.ContentView;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * Created by user on 2017/6/12.
 * author:tian
 */

public abstract class BaseActivity<UC> extends CoreActivity<UC>{

    @Nullable
    @BindView(R.id.toolbar)
    Toolbar mToolbar;



    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        ButterKnife.bind(this);
        initializeToolbar();
        handleIntent(getIntent(),getDisplay());
        initializeViews(savedInstanceState);

    }

    @Override
    protected void onNewIntent(Intent intent) {
        super.onNewIntent(intent);
        handleIntent(intent,getDisplay());
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
    }

    //获取布局的id
    protected int getLayoutId(){
        for (Class c = getClass(); c != Context.class; c = c.getSuperclass()){
            ContentView annotation = (ContentView) c.getAnnotation(ContentView.class);
            if (annotation != null){
                return annotation.value();
            }
        }
        return 0;
    }

    //初始化Toolbar
    private void  initializeToolbar(){
        if (mToolbar != null){
            mToolbar.setTitle(getTitle());
            setSupportActionBar(mToolbar);
            getSupportActionBar().setHomeButtonEnabled(true);
            getSupportActionBar().setDisplayHomeAsUpEnabled(true);
        }
    };

    //事件接口
    protected void handleIntent(Intent intent, Display display){}

    //初始化控件接口
    protected void initializeViews(Bundle savedInstanceState){}

    //设置标题
    @Override
    public void setTitle(CharSequence title) {
        if (mToolbar != null){
            mToolbar.setTitle(title);
        }
    }

    @Override
    public void setTitle(@StringRes int titleId) {
        super.setTitle(titleId);
    }


}
